<template>
	<view class="info-input">
		<view class="item">
			<view class="title">{{dataObj.title}}</view>
			<input class="content" v-model="dataObj.value" :maxlength="maxlength" :placeholder="'请输入' + dataObj.title" />
		</view>
		<view class="footer">
			<view class="btn" @click="onPostTap">确定</view>
		</view>
	</view>
</template>

<script>
	import { requestErrorTip, checkMobileReg, getInfoData } from '@/common/tools.js';
	
	export default {
		data() {
			return {
				dataObj: {
					type: '',
					title: '',
					maxlength: -1,
					value: ''
				}
			}
		},
		onLoad(event) {
			this.dataObj['type'] = event['type']; // 获取页面数据类型
			this.initPage();
		},
		methods: {
			// 初始化页面
			initPage() {
				const type = this.dataObj['type'];
				let title = '';
				let maxlength = -1;
				switch(type) {
					case 'nickname':
						title = '姓名';
						maxlength = 20;
						break;
					case 'mobile':
						title = '手机号';
						maxlength = 11;
						break;
					case 'identity_id':
						title = '身份证号';
						maxlength = 18;
						break;
					case 'company_name':
						title = '工作单位';
						maxlength = 50;
						break;
					case 'role_name':
						title = '职业名称';
						maxlength = 20;
						break;
					case 'jobName':
						title = '工种名称';
						maxlength = 50;
						break;
					default:
						break;
				}
				this.dataObj['title'] = title;
				this.dataObj['maxlength'] = maxlength;
				uni.setNavigationBarTitle({
				　　title: title
				})
			},
			// 确定提交
			onPostTap() {
				const pData = {};
				const value = this.dataObj['value'];
				const type = this.dataObj['type'];
				if(value === '') {
					requestErrorTip('toast', '输入框不能为空');
					return false;
				}
				if (type === 'nickname' && value.length > 20) {
					requestErrorTip('toast', '姓名不能超过20个字节');
					return false; 
				}
				if (type === 'mobile' && !checkMobileReg(value)) {
					requestErrorTip('toast', '请输入正确的手机号');
					return false;
				}
				if (type === 'identity_id') {
					const reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; 
					if(reg.test(value) === false) { 
						requestErrorTip('toast', '身份证输入不合法');
						return false; 
					} 
				}
				pData[type] = value;
				this.$Requset({
					url: this.$Api['user']['editinfo'],
					data: pData,
					success: (res) => {
						const { data, status, msg } = res;
						if (!status) {
							requestErrorTip('toast', msg);
							return;
						}
						// 重新获取并存储个人信息数据
						getInfoData(() => {
							uni.navigateBack();
						});
					},
					fail: (err) => {
						console.log(err);
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #f5f5f5;
	}
	.info-input{
		padding-top: 18rpx;
		.item{
			position: relative;
			height: 40rpx;
			padding: 32rpx 36rpx 32rpx 180rpx;
			background-color: #FFFFFF;
			.title{
				box-sizing: border-box;
				position: absolute;
				top: 32rpx;
				left: 0;
				width: 180rpx;
				height: 40rpx;
				line-height: 40rpx;
				font-size: 30rpx;
				color: #1A1A1A;
				padding-left: 36rpx;
			}
			.content{
				height: 40rpx;
				line-height: 40rpx;
				font-size: 30rpx;
				color: #7F7F7F;
			}
		}
		.footer{
			padding: 36rpx;
			.btn{
				width: 100%;
				height: 82rpx;
				line-height: 82rpx;
				font-size: 32rpx;
				color: #FFFFFF;
				text-align: center;
				background-color: $color-common;
				border-radius: 41rpx;
			}
		}
	}
</style>
